
use  "created_dta/fullsample_tcdmsl_withdemo", clear
g dur_army_since_post_arriv = (last_ym - date_of_arrival_1)/12
g tc_orgraa = firstpost_orgraa_ind
g state_orgraa = ""
g post_orgraa = ""
replace tc_orgraa = "KN" if tc_orgraa == "K6" // both korea
replace tc_orgraa = "G3" if tc_orgraa == "OE" 
replace tc_orgraa = "IV" if tc_orgraa == "IT" // both italy
replace tc_orgraa = "HS" if tc_orgraa == "15" // Hawaii
replace tc_orgraa = "AD" if tc_orgraa == "02" // Alaska
replace tc_orgraa = "KK" if tc_orgraa == "TH" & dmsl_tc_first == "1I3_TC" 
replace tc_orgraa = "NR" if tc_orgraa == "KR" & dmsl_tc_first == "21M_TC" 
replace tc_orgraa = "CC" if tc_orgraa == "KN" & dmsl_tc_first == "22I_TC" 
replace tc_orgraa = "WL" if tc_orgraa == "98" & dmsl_tc_first == "42I_TC" 

replace state_orgraa = "KY" if tc_orgraa == "KC"
replace post_orgraa = "Fort Campbell" if tc_orgraa == "KC"

replace state_orgraa = "TX" if tc_orgraa == "TB"
replace post_orgraa = "Fort Bliss" if tc_orgraa == "TB"

replace state_orgraa = "TX" if tc_orgraa == "TH"
replace post_orgraa = "Fort Hood" if tc_orgraa == "TH"

replace state_orgraa = "KS" if tc_orgraa == "KR"
replace post_orgraa = "Fort Riley" if tc_orgraa == "KR"

replace state_orgraa = "NY" if tc_orgraa == "NR"
replace post_orgraa = "Fort Drum" if tc_orgraa == "NR"

replace state_orgraa = "KOREA" if tc_orgraa == "KN"
replace post_orgraa = "KOREA" if tc_orgraa == "KN"

replace state_orgraa = "GA" if tc_orgraa == "GS"
replace post_orgraa = "Fort Stewart" if tc_orgraa == "GS"

replace state_orgraa = "CO" if tc_orgraa == "CC"
replace post_orgraa = "Fort Carson" if tc_orgraa == "CC"

replace state_orgraa = "GERMANY" if tc_orgraa == "G3"
replace post_orgraa = "GERMANY" if tc_orgraa == "G3"

replace state_orgraa = "ITALY" if tc_orgraa == "IV"
replace post_orgraa = "ITALY" if tc_orgraa == "IV"

replace state_orgraa = "NC" if tc_orgraa == "NB"
replace post_orgraa = "Fort Bragg" if tc_orgraa == "NB"

replace state_orgraa = "KY" if tc_orgraa == "KK"
replace post_orgraa = "Fort Knox" if tc_orgraa == "KK"

replace state_orgraa = "HI" if tc_orgraa == "HS"
replace post_orgraa = "Schofield Barracks" if tc_orgraa == "HS"

replace state_orgraa = "WA" if tc_orgraa == "WL"
replace post_orgraa = "Fort Lewis" if tc_orgraa == "WL"

replace state_orgraa = "GA" if tc_orgraa == "GB"
replace post_orgraa = "Fort Benning" if tc_orgraa == "GB"

replace state_orgraa = "LA" if tc_orgraa == "LP"
replace post_orgraa = "Fort Polk" if tc_orgraa == "LP"

replace state_orgraa = "AK" if tc_orgraa == "AD"
replace post_orgraa = "Fort Richardson" if tc_orgraa == "AD"

replace state_orgraa = "AK" if tc_orgraa == "AW"
replace post_orgraa = "Fort Wainwright" if tc_orgraa == "AW"

tab post_orgraa, m
tab state_orgraa, m
tab tc_orgraa, m

g year_arrival =  year(dofm(date_of_arrival_1))

g bct_type = ""

replace bct_type="IBCT" if dmsl_tc=="101_TC"
replace bct_type="IBCT" if dmsl_tc=="201_TC"
replace bct_type="IBCT" if dmsl_tc=="301_TC"
replace bct_type="IBCT" if dmsl_tc=="401_TC"



replace bct_type="ABCT" if dmsl_tc=="11A_TC"&year_arrival>=2008&year_arrival<=2010
replace bct_type="SBCT" if dmsl_tc=="11A_TC"&year_arrival>=2011&year_arrival<=2018
replace bct_type="ABCT" if dmsl_tc=="11A_TC"&year_arrival>=2019
replace bct_type="ABCT" if dmsl_tc=="21A_TC"
replace bct_type="IBCT" if dmsl_tc=="31A_TC"



replace bct_type="ABCT" if dmsl_tc=="11C_TC"
replace bct_type="ABCT" if dmsl_tc=="21C_TC"
replace bct_type="ABCT" if dmsl_tc=="31C_TC"
replace bct_type="ABCT" if dmsl_tc=="41C_TC"



replace bct_type="IBCT" if dmsl_tc=="11M_TC"
replace bct_type="IBCT" if dmsl_tc=="21M_TC"
replace bct_type="IBCT" if dmsl_tc=="31M_TC"
replace bct_type="IBCT" if dmsl_tc=="41M_TC"



replace bct_type="AR_BDE" if dmsl_tc=="11I_TC"
replace bct_type="AR_BDE" if dmsl_tc=="21I_TC"
replace bct_type="AR_BDE" if dmsl_tc=="31I_TC"
replace bct_type="IBCT" if dmsl_tc=="41I_TC"
replace bct_type="ABCT" if dmsl_tc=="11IV2_TC"
replace bct_type="ABCT" if dmsl_tc=="21IV2_TC"



replace bct_type="ABCT" if dmsl_tc=="12I_TC"
replace bct_type="IBCT" if dmsl_tc=="22I_TC"
replace bct_type="SBCT" if dmsl_tc=="32I_TC"
replace bct_type="SBCT" if dmsl_tc=="42I_TC"
replace bct_type="SBCT" if dmsl_tc=="52I_TC"



replace bct_type="ABCT" if dmsl_tc=="13I_TC"
replace bct_type="ABCT" if dmsl_tc=="23I_TC"
replace bct_type="ABCT" if dmsl_tc=="33I_TC"
replace bct_type="IBCT" if dmsl_tc=="43I_TC"&year_arrival<=2017
replace bct_type="ABCT" if dmsl_tc=="43I_TC"&year_arrival>=2018



replace bct_type="IBCT" if dmsl_tc=="182_TC"
replace bct_type="IBCT" if dmsl_tc=="282_TC"
replace bct_type="IBCT" if dmsl_tc=="382_TC"
replace bct_type="IBCT" if dmsl_tc=="482_TC"



replace bct_type="IBCT" if dmsl_tc=="225_TC"&year_arrival<2005
replace bct_type="SBCT" if dmsl_tc=="225_TC"&year_arrival>=2005&year_arrival<=2016
replace bct_type="IBCT" if dmsl_tc=="225_TC"&year_arrival>=2017
replace bct_type="IBCT" if dmsl_tc=="325_TC"
replace bct_type="IBCT" if dmsl_tc=="425_TC"



replace bct_type="ABCT" if dmsl_tc=="14I_TC"&year_arrival<=2013
replace bct_type="SBCT" if dmsl_tc=="14I_TC"&year_arrival>=2014
replace bct_type="ABCT" if dmsl_tc=="24I_TC"
replace bct_type="ABCT" if dmsl_tc=="34I_TC"
replace bct_type="ABCT" if dmsl_tc=="44I_TC"



replace bct_type="MECH_BDE" if dmsl_tc=="170_TC"
replace bct_type="MECH_BDE" if dmsl_tc=="172_TC"
replace bct_type="IBCT" if dmsl_tc=="173_TC"
replace bct_type="AR_BDE" if dmsl_tc=="1A1_TC"
replace bct_type="IBCT" if dmsl_tc=="1I3_TC"
replace bct_type="ACR" if dmsl_tc=="3AC_TC"&year_arrival<=2011

replace bct_type="ABCT" if dmsl_tc=="3CR_TC"&year_arrival<=2010
replace bct_type="SBCT" if dmsl_tc=="3CR_TC"&year_arrival>=2011

replace bct_type="SBCT" if dmsl_tc=="81F_TC"
replace bct_type="SBCT" if dmsl_tc=="SB1_TC"

g ym_conspmos = date_of_arrival_1
g pmos_conspmos = init_pmos
merge m:1 pmos_conspmos ym_conspmos using "raw_dta/pmos-to-conspmos-jan2021-panel.dta"
drop if _merge == 2
g init_pmos_cons = conspmos
replace init_pmos_cons = init_pmos + "_imp" if init_pmos_cons == ""

replace init_pmos_cons="09B" if init_pmos_cons=="09B_imp"
replace init_pmos_cons="12B" if init_pmos_cons=="21B_imp"
replace init_pmos_cons="15W" if init_pmos_cons=="96U_imp"
replace init_pmos_cons="91A" if init_pmos_cons=="63A_imp"
replace init_pmos_cons="91B" if init_pmos_cons=="63B_imp"
replace init_pmos_cons="91H" if init_pmos_cons=="63H_imp"
replace init_pmos_cons="91M" if init_pmos_cons=="63M_imp"
replace init_pmos_cons="91D" if init_pmos_cons=="52D_imp"
replace init_pmos_cons="68W" if init_pmos_cons=="91W_imp"
replace init_pmos_cons="35F" if init_pmos_cons=="96B_imp"
replace init_pmos_cons="35M" if init_pmos_cons=="97E_imp"
replace init_pmos_cons="25U" if init_pmos_cons=="31U_imp"
replace init_pmos_cons="25S" if init_pmos_cons=="31S_imp"
replace init_pmos_cons="35T" if init_pmos_cons=="33W_imp"
replace init_pmos_cons="15R" if init_pmos_cons=="35K_imp"
replace init_pmos_cons="14G" if init_pmos_cons=="14G_imp"
 
	order shr_hfp_loo_m2 shr_hfp_loo_m1 shr_hfp_loo_0 shr_hfp_loo_1 ///
	shr_hfp_loo_2 shr_hfp_loo_3 shr_hfp_loo_4 shr_hfp_loo_5 shr_hfp_loo_6 ///
	shr_hfp_loo_7 shr_hfp_loo_8 shr_hfp_loo_9 shr_hfp_loo_1* shr_hfp_loo_2*
	
forvalues x = 1(1)36 {
g missing_`x' = shr_hfp_loo_`x'==.
}
egen evermissing = rowtotal(missing_*)

forvalues x = 1(1)36 {
g counts_`x' = shr_hfp_loo_`x' >= 0.6 & shr_hfp_loo_`x' < .
}
egen count_inst_3yr = rowtotal(counts_*)
replace count_inst_3yr = . if evermissing > 1

replace count_inst_3yr = count_inst_3yr/36
drop counts_* missing_* evermissing 

forvalues x = 1(1)24 {
g missing_`x' = shr_hfp_loo_`x'==.
}
egen evermissing = rowtotal(missing_*)

forvalues x = 1(1)24 {
g counts_`x' = shr_hfp_loo_`x' >= 0.6 & shr_hfp_loo_`x' < .
}
egen count_inst_2yr = rowtotal(counts_*)
replace count_inst_2yr = . if evermissing > 1

replace count_inst_2yr = count_inst_2yr/24
drop counts_* missing_* evermissing 

tab date_of_arrival_1 if count_inst_2yr != . 
summ any_hfp* if count_inst_2yr != . 
summ months_hfp* if count_inst_2yr != . 
summ months_hfp_firstpost if any_hfp_firstpost & count_inst_2yr != . 

tab date_of_arrival_1 if count_inst_3yr != . 
summ any_hfp* if count_inst_3yr != . 
summ months_hfp* if count_inst_3yr != . 
summ months_hfp_firstpost if any_hfp_firstpost & count_inst_3yr != . 

g entry_yr = year(dofm(date_of_arrival_1))

g black = init_race == "B"
g hisp = init_race == "H"
g other_race = init_race == "X"
g white = init_race == "W"
g female = init_sex == "F"

g married = init_marst == "M"
g anydep = init_nrdep >= 1 & init_nrdep < .

g init_dob_ym = ym(year(init_dobym_s), month(init_dobym_s))
g init_approx_age = (date_of_arrival - init_dob_ym)/12

egen ed_cats = group(init_civedcatg)
replace ed_cats = 8 if ed_cats == .

g reenlist = 0
replace reenlist = 1 if init_terms=="3" & dur_army > 3 
replace reenlist = 1 if init_terms=="4" & dur_army > 4
replace reenlist = 1 if init_terms=="5" & dur_army > 5
replace reenlist = 1 if init_terms=="6" & dur_army > 6

g dep_win_4yr = (deployed_0 == 1 | deployed_1 == 1 | deployed_2 == 1  | deployed_3 == 1 /// 
| deployed_4 == 1 | deployed_5 == 1 | deployed_6 == 1  | deployed_7 == 1 /// 
| deployed_8 == 1 | deployed_9 == 1 | deployed_10 == 1  | deployed_11 == 1 ///
| deployed_12 == 1 | deployed_13 == 1 | deployed_14 == 1  | deployed_15 == 1  ///
| deployed_16 == 1 | deployed_17 == 1 | deployed_18 == 1  | deployed_19 == 1  ///
| deployed_20 == 1 | deployed_21 == 1 | deployed_22 == 1  | deployed_23 == 1 ///
| deployed_24 == 1 | deployed_25 == 1 | deployed_26 == 1  | deployed_27 == 1  ///
| deployed_28 == 1 | deployed_29 == 1 | deployed_30 == 1  | deployed_31 == 1  ///
| deployed_32 == 1 | deployed_33 == 1 | deployed_34 == 1  | deployed_35 == 1 ///
| deployed_36 == 1 | deployed_37 == 1 | deployed_38 == 1  | deployed_39 == 1  ///
| deployed_40 == 1 | deployed_41 == 1 | deployed_42 == 1  | deployed_43 == 1  ///
| deployed_44 == 1 | deployed_45 == 1 | deployed_46 == 1  | deployed_47 == 1)


g dep_win_5yr = (deployed_0 == 1 | deployed_1 == 1 | deployed_2 == 1  | deployed_3 == 1 /// 
| deployed_4 == 1 | deployed_5 == 1 | deployed_6 == 1  | deployed_7 == 1 /// 
| deployed_8 == 1 | deployed_9 == 1 | deployed_10 == 1  | deployed_11 == 1 ///
| deployed_12 == 1 | deployed_13 == 1 | deployed_14 == 1  | deployed_15 == 1  ///
| deployed_16 == 1 | deployed_17 == 1 | deployed_18 == 1  | deployed_19 == 1  ///
| deployed_20 == 1 | deployed_21 == 1 | deployed_22 == 1  | deployed_23 == 1 ///
| deployed_24 == 1 | deployed_25 == 1 | deployed_26 == 1  | deployed_27 == 1  ///
| deployed_28 == 1 | deployed_29 == 1 | deployed_30 == 1  | deployed_31 == 1  ///
| deployed_32 == 1 | deployed_33 == 1 | deployed_34 == 1  | deployed_35 == 1 ///
| deployed_36 == 1 | deployed_37 == 1 | deployed_38 == 1  | deployed_39 == 1  ///
| deployed_40 == 1 | deployed_41 == 1 | deployed_42 == 1  | deployed_43 == 1  ///
| deployed_44 == 1 | deployed_45 == 1 | deployed_46 == 1  | deployed_47 == 1 ///
| deployed_48 == 1 | deployed_49 == 1 | deployed_50 == 1  | deployed_51 == 1  ///
| deployed_52 == 1 | deployed_53 == 1 | deployed_54 == 1  | deployed_55 == 1  ///
| deployed_56 == 1 | deployed_57 == 1 | deployed_58 == 1  | deployed_59 == 1)

g dep_firstterm = .
replace dep_firstterm = dep_win_3yr if init_terms == "3"
replace dep_firstterm = dep_win_4yr if init_terms == "4"
replace dep_firstterm = dep_win_5yr if init_terms == "5"
replace dep_firstterm = dep_win_5yr if init_terms == "6"

g month_arrival = month(dofm(date_of_arrival_1))
g quarter_arrival = 1 if inlist(month_arrival,1,2,3)
replace quarter_arrival = 2 if inlist(month_arrival,4,5,6)
replace quarter_arrival = 3 if inlist(month_arrival,7,8,9)
replace quarter_arrival = 4 if inlist(month_arrival,10,11,12)
egen YQ = group(year_arrival quarter_arrival)

egen BCTXarrivmonth = group(dmsl_tc_first date_of)
egen BCTXarrivquarter = group(dmsl_tc_first year_arrival quarter_arrival)
egen BCTXarrivyr = group(dmsl_tc_first year_arrival)

count if init_pmos == ""
count if init_grade == ""
count if init_terms == ""
count if entry_yr == .
count if init_yos == .

g afqs_mi = init_afqsc == .
replace init_afqsc = 0 if init_afqsc == . 

keep if date_of_arrival_1 < ym(2016,1)
sum date_of_arrival_1 
drop if entry_yr == .   
drop if init_yos == .   
keep if init_yos == 0   
drop if init_dobym_s<d(1jan1930)  
count 
egen BCT=group(dmsl_tc_first)

g serve_mt_1 = dur_army > 1
g serve_mt_2 = dur_army > 2
g serve_mt_3 = dur_army > 3
g serve_mt_4 = dur_army > 4
g serve_mt_5 = dur_army > 5

forvalues x = 1(1)19 {
   g sep_`x' = dur_army >= 0.25*(`x'-1) &  dur_army < 0.25*(`x')
}

egen months_hfp_1yr = rowtotal(deployed_1-deployed_12)
egen months_hfp_2yr = rowtotal(deployed_1-deployed_24)
egen months_hfp_3yr = rowtotal(deployed_1-deployed_36)

g binary_inst_2yr = count_inst_2yr>0
g binary_inst_3yr = count_inst_3yr>0
g hsgplus = inlist(init_civedcatg,"HSG","SMC","ASC","CLG")

egen postorgraa = group(post_orgraa)
egen initpmoscons = group(init_pmos_cons)
egen initgrade = group(init_grade)
egen initterms = group(init_terms)
egen initstatehor = group(init_statehor)
egen bcttype = group(bct_type)

gen any_asgcons=(init_asgcons!="")

gen abn_volunteer=(init_asgcons=="D8"|init_asgcons=="V5")
gen child_soldier=(init_asgcons=="U8")
gen init_age_round=round(init_approx_age,0.1)

gen other_asgcons=(any_asgcons==1 & abn_volunteer==0 & child_soldier==0)

gen init_phyca_norestrict=(init_phyca=="A")
gen init_phyca_nosignif=(init_phyca=="B")
gen init_phyca_limitations=(init_phyca!="" & init_phyca_norestrict==0 & init_phyca_nosignif==0)
gen init_phyca_missing=(init_phyca=="")
assert init_phyca_norestrict+init_phyca_nosignif+init_phyca_limitations+init_phyca_missing==1

egen mos_post_month=group(init_pmos_cons post_orgraa year_arrival month_arrival)
egen mos_post_qtr=group(init_pmos_cons post_orgraa year_arrival quarter_arrival)

egen post_qtr=group( post_orgraa year_arrival quarter_arrival)
egen mos_qtr=group( init_pmos_cons year_arrival quarter_arrival)

egen post_month = group( post_orgraa year_arrival month_arrival)
egen mos_month = group( init_pmos_cons year_arrival month_arrival)


egen mos_post_qtr_initterms=group(mos_post_qtr initterms)
egen mos_post_qtr_bcttype_initterms=group(mos_post_qtr bct_type initterms)


**************** SAMPLE RESTRICTIONS ****************

drop if init_terms==""
drop if inlist(init_terms,"0","1","2")
keep if inlist(init_terms,"3","4","5","6") 
tab init_grade, m
keep if inlist(init_grade,"E01","E02","E03","E04")
drop if init_phyca_limitations==1 | other_asgcons==1 | child_soldier==1
drop if inlist(post_orgraa,"GERMANY","ITALY","KOREA")
drop if post_orgraa=="Fort Polk" & dmsl_tc_first=="42I_TC" 
drop if post_orgraa=="Fort Hood" & dmsl_tc_first=="24I_TC"
drop if post_orgraa=="Fort Hood" & dmsl_tc_first=="14I_TC"
drop if post_orgraa=="Fort Lewis" & dmsl_tc_first=="81F_TC"
drop if post_orgraa=="Fort Richardson" & dmsl_tc_first=="SB1_TC" 
drop if count_enlisted_36==.|count_enlisted_36<2000
preserve
keep post_qtr post_orgraa dmsl_tc_first pid
duplicates drop post_qtr post_orgraa dmsl_tc_first, force
sort post_qtr post_orgraa dmsl_tc_first
bysort post_qtr: egen dmsl_ct_inpost=count(dmsl_tc_first)
tempfile only_bct_post
save `only_bct_post'
restore
merge m:1 post_qtr dmsl_tc_first using `only_bct_post', keep(1 3) keepusing(dmsl_ct_inpost) nogen
drop if dmsl_ct_inpost==1
preserve
keep post_orgraa dmsl_tc_first 
duplicates drop post_orgraa dmsl_tc_first, force
bys dmsl_tc_first: egen post_ct=count(post_orgraa)
tempfile post_ct
save `post_ct'
restore
merge m:1 dmsl_tc_first using `post_ct', keep(1 3) keepusing(post_ct) nogen
assert post_ct==1
drop post_ct
egen BCT_ct=count(pid), by(BCT)
egen BCTXarrivyr_ct=count(pid), by(BCTXarrivyr)
drop if BCTXarrivyr_ct<100
g c = 1 
bys init_pmos_cons: egen init_pmos_cons_ct = total(c)
tab init_pmos_cons_ct
drop if inlist(init_pmos_cons,"09B","09S_imp","98X_imp","00D_imp","00G_imp")
drop if inlist(init_pmos_cons,"CAC_imp","CFB_imp","XXX_imp","YYO_imp")
drop if substr(init_pmos_cons,5,3)=="imp"
tab post_orgraa 
tab init_pmos_cons
compress
save "created_dta\analysis_samp", replace